home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bison / bisn119s.zoo / bison.info-1 < prev    next >
Encoding:
GNU Info File  |  1992-09-05  |  49.3 KB  |  1,097 lines

  1. This is Info file bison.info, produced by Makeinfo-1.47 from the input
  2. file /home/gd/gnu/bison/bison.texinfo.
  3.  
  4.    This file documents the Bison parser generator.
  5.  
  6.    Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the sections entitled "GNU General Public License" and "Conditions
  15. for Using Bison" are included exactly as in the original, and provided
  16. that the entire resulting derived work is distributed under the terms
  17. of a permission notice identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the sections entitled "GNU General Public
  22. License", "Conditions for Using Bison" and this permission notice may be
  23. included in translations approved by the Free Software Foundation
  24. instead of in the original English.
  25.  
  26. File: bison.info,  Node: Top,  Next: Introduction,  Prev: (DIR),  Up: (DIR)
  27.  
  28.    This manual documents version 1.19 of Bison.
  29.  
  30. * Menu:
  31.  
  32. * Introduction::
  33. * Conditions::
  34. * Copying::           The GNU General Public License says
  35.                         how you can copy and share Bison
  36.  
  37. Tutorial sections:
  38. * Concepts::          Basic concepts for understanding Bison.
  39. * Examples::          Three simple explained examples of using Bison.
  40.  
  41. Reference sections:
  42. * Grammar File::      Writing Bison declarations and rules.
  43. * Interface::         C-language interface to the parser function `yyparse'.
  44. * Algorithm::         How the Bison parser works at run-time.
  45. * Error Recovery::    Writing rules for error recovery.
  46. * Context Dependency::What to do if your language syntax is too
  47.                         messy for Bison to handle straightforwardly.
  48. * Debugging::         Debugging Bison parsers that parse wrong.
  49. * Invocation::        How to run Bison (to produce the parser source file).
  50. * Table of Symbols::  All the keywords of the Bison language are explained.
  51. * Glossary::          Basic concepts are explained.
  52. * Index::             Cross-references to the text.
  53.  
  54. File: bison.info,  Node: Introduction,  Next: Conditions,  Prev: Top,  Up: Top
  55.  
  56. Introduction
  57. ************
  58.  
  59.    "Bison" is a general-purpose parser generator that converts a
  60. grammar description for an LALR(1) context-free grammar into a C
  61. program to parse that grammar.  Once you are proficient with Bison, you
  62. may use it to develop a wide range of language parsers, from those used
  63. in simple desk calculators to complex programming languages.
  64.  
  65.    Bison is upward compatible with Yacc: all properly-written Yacc
  66. grammars ought to work with Bison with no change.  Anyone familiar with
  67. Yacc should be able to use Bison with little trouble.  You need to be
  68. fluent in C programming in order to use Bison or to understand this
  69. manual.
  70.  
  71.    We begin with tutorial chapters that explain the basic concepts of
  72. using Bison and show three explained examples, each building on the
  73. last.  If you don't know Bison or Yacc, start by reading these
  74. chapters.  Reference chapters follow which describe specific aspects of
  75. Bison in detail.
  76.  
  77.    Bison was written primarily by Robert Corbett; Richard Stallman made
  78. it Yacc-compatible.  This edition corresponds to version 1.19 of Bison.
  79.  
  80. File: bison.info,  Node: Conditions,  Next: Copying,  Prev: Introduction,  Up: Top
  81.  
  82. Conditions for Using Bison
  83. **************************
  84.  
  85.    Bison grammars can be used only in programs that are free software. 
  86. This is in contrast to what happens with the GNU C compiler and the
  87. other GNU programming tools.
  88.  
  89.    The reason Bison is special is that the output of the Bison
  90. utility--the Bison parser file--contains a verbatim copy of a sizable
  91. piece of Bison, which is the code for the `yyparse' function.  (The
  92. actions from your grammar are inserted into this function at one point,
  93. but the rest of the function is not changed.)
  94.  
  95.    As a result, the Bison parser file is covered by the same copying
  96. conditions that cover Bison itself and the rest of the GNU system: any
  97. program containing it has to be distributed under the standard GNU
  98. copying conditions.
  99.  
  100.    Occasionally people who would like to use Bison to develop
  101. proprietary programs complain about this.
  102.  
  103.    We don't particularly sympathize with their complaints.  The purpose
  104. of the GNU project is to promote the right to share software and the
  105. practice of sharing software; it is a means of changing society.  The
  106. people who complain are planning to be uncooperative toward the rest of
  107. the world; why should they deserve our help in doing so?
  108.  
  109.    However, it's possible that a change in these conditions might
  110. encourage computer companies to use and distribute the GNU system.  If
  111. so, then we might decide to change the terms on `yyparse' as a matter
  112. of the strategy of promoting the right to share.  Such a change would be
  113. irrevocable.  Since we stand by the copying permissions we have
  114. announced, we cannot withdraw them once given.
  115.  
  116.    We mustn't make an irrevocable change hastily.  We have to wait
  117. until there is a complete GNU system and there has been time to learn
  118. how this issue affects its reception.
  119.  
  120. File: bison.info,  Node: Copying,  Next: Concepts,  Prev: Conditions,  Up: Top
  121.  
  122. GNU GENERAL PUBLIC LICENSE
  123. **************************
  124.  
  125.                          Version 2, June 1991
  126.  
  127.      Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  128.      675 Mass Ave, Cambridge, MA 02139, USA
  129.      
  130.      Everyone is permitted to copy and distribute verbatim copies
  131.      of this license document, but changing it is not allowed.
  132.  
  133. Preamble
  134. ========
  135.  
  136.    The licenses for most software are designed to take away your
  137. freedom to share and change it.  By contrast, the GNU General Public
  138. License is intended to guarantee your freedom to share and change free
  139. software--to make sure the software is free for all its users.  This
  140. General Public License applies to most of the Free Software
  141. Foundation's software and to any other program whose authors commit to
  142. using it.  (Some other Free Software Foundation software is covered by
  143. the GNU Library General Public License instead.)  You can apply it to
  144. your programs, too.
  145.  
  146.    When we speak of free software, we are referring to freedom, not
  147. price.  Our General Public Licenses are designed to make sure that you
  148. have the freedom to distribute copies of free software (and charge for
  149. this service if you wish), that you receive source code or can get it
  150. if you want it, that you can change the software or use pieces of it in
  151. new free programs; and that you know you can do these things.
  152.  
  153.    To protect your rights, we need to make restrictions that forbid
  154. anyone to deny you these rights or to ask you to surrender the rights.
  155. These restrictions translate to certain responsibilities for you if you
  156. distribute copies of the software, or if you modify it.
  157.  
  158.    For example, if you distribute copies of such a program, whether
  159. gratis or for a fee, you must give the recipients all the rights that
  160. you have.  You must make sure that they, too, receive or can get the
  161. source code.  And you must show them these terms so they know their
  162. rights.
  163.  
  164.    We protect your rights with two steps: (1) copyright the software,
  165. and (2) offer you this license which gives you legal permission to copy,
  166. distribute and/or modify the software.
  167.  
  168.    Also, for each author's protection and ours, we want to make certain
  169. that everyone understands that there is no warranty for this free
  170. software.  If the software is modified by someone else and passed on, we
  171. want its recipients to know that what they have is not the original, so
  172. that any problems introduced by others will not reflect on the original
  173. authors' reputations.
  174.  
  175.    Finally, any free program is threatened constantly by software
  176. patents.  We wish to avoid the danger that redistributors of a free
  177. program will individually obtain patent licenses, in effect making the
  178. program proprietary.  To prevent this, we have made it clear that any
  179. patent must be licensed for everyone's free use or not licensed at all.
  180.  
  181.    The precise terms and conditions for copying, distribution and
  182. modification follow.
  183.  
  184.     TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  185.  
  186.   1. This License applies to any program or other work which contains a
  187.      notice placed by the copyright holder saying it may be distributed
  188.      under the terms of this General Public License.  The "Program",
  189.      below, refers to any such program or work, and a "work based on
  190.      the Program" means either the Program or any derivative work under
  191.      copyright law: that is to say, a work containing the Program or a
  192.      portion of it, either verbatim or with modifications and/or
  193.      translated into another language.  (Hereinafter, translation is
  194.      included without limitation in the term "modification".)  Each
  195.      licensee is addressed as "you".
  196.  
  197.      Activities other than copying, distribution and modification are
  198.      not covered by this License; they are outside its scope.  The act
  199.      of running the Program is not restricted, and the output from the
  200.      Program is covered only if its contents constitute a work based on
  201.      the Program (independent of having been made by running the
  202.      Program). Whether that is true depends on what the Program does.
  203.  
  204.   2. You may copy and distribute verbatim copies of the Program's
  205.      source code as you receive it, in any medium, provided that you
  206.      conspicuously and appropriately publish on each copy an appropriate
  207.      copyright notice and disclaimer of warranty; keep intact all the
  208.      notices that refer to this License and to the absence of any
  209.      warranty; and give any other recipients of the Program a copy of
  210.      this License along with the Program.
  211.  
  212.      You may charge a fee for the physical act of transferring a copy,
  213.      and you may at your option offer warranty protection in exchange
  214.      for a fee.
  215.  
  216.   3. You may modify your copy or copies of the Program or any portion
  217.      of it, thus forming a work based on the Program, and copy and
  218.      distribute such modifications or work under the terms of Section 1
  219.      above, provided that you also meet all of these conditions:
  220.  
  221.        a. You must cause the modified files to carry prominent notices
  222.           stating that you changed the files and the date of any change.
  223.  
  224.        b. You must cause any work that you distribute or publish, that
  225.           in whole or in part contains or is derived from the Program
  226.           or any part thereof, to be licensed as a whole at no charge
  227.           to all third parties under the terms of this License.
  228.  
  229.        c. If the modified program normally reads commands interactively
  230.           when run, you must cause it, when started running for such
  231.           interactive use in the most ordinary way, to print or display
  232.           an announcement including an appropriate copyright notice and
  233.           a notice that there is no warranty (or else, saying that you
  234.           provide a warranty) and that users may redistribute the
  235.           program under these conditions, and telling the user how to
  236.           view a copy of this License.  (Exception: if the Program
  237.           itself is interactive but does not normally print such an
  238.           announcement, your work based on the Program is not required
  239.           to print an announcement.)
  240.  
  241.      These requirements apply to the modified work as a whole.  If
  242.      identifiable sections of that work are not derived from the
  243.      Program, and can be reasonably considered independent and separate
  244.      works in themselves, then this License, and its terms, do not
  245.      apply to those sections when you distribute them as separate
  246.      works.  But when you distribute the same sections as part of a
  247.      whole which is a work based on the Program, the distribution of
  248.      the whole must be on the terms of this License, whose permissions
  249.      for other licensees extend to the entire whole, and thus to each
  250.      and every part regardless of who wrote it.
  251.  
  252.      Thus, it is not the intent of this section to claim rights or
  253.      contest your rights to work written entirely by you; rather, the
  254.      intent is to exercise the right to control the distribution of
  255.      derivative or collective works based on the Program.
  256.  
  257.      In addition, mere aggregation of another work not based on the
  258.      Program with the Program (or with a work based on the Program) on
  259.      a volume of a storage or distribution medium does not bring the
  260.      other work under the scope of this License.
  261.  
  262.   4. You may copy and distribute the Program (or a work based on it,
  263.      under Section 2) in object code or executable form under the terms
  264.      of Sections 1 and 2 above provided that you also do one of the
  265.      following:
  266.  
  267.        a. Accompany it with the complete corresponding machine-readable
  268.           source code, which must be distributed under the terms of
  269.           Sections 1 and 2 above on a medium customarily used for
  270.           software interchange; or,
  271.  
  272.        b. Accompany it with a written offer, valid for at least three
  273.           years, to give any third party, for a charge no more than your
  274.           cost of physically performing source distribution, a complete
  275.           machine-readable copy of the corresponding source code, to be
  276.           distributed under the terms of Sections 1 and 2 above on a
  277.           medium customarily used for software interchange; or,
  278.  
  279.        c. Accompany it with the information you received as to the offer
  280.           to distribute corresponding source code.  (This alternative is
  281.           allowed only for noncommercial distribution and only if you
  282.           received the program in object code or executable form with
  283.           such an offer, in accord with Subsection b above.)
  284.  
  285.      The source code for a work means the preferred form of the work for
  286.      making modifications to it.  For an executable work, complete
  287.      source code means all the source code for all modules it contains,
  288.      plus any associated interface definition files, plus the scripts
  289.      used to control compilation and installation of the executable. 
  290.      However, as a special exception, the source code distributed need
  291.      not include anything that is normally distributed (in either
  292.      source or binary form) with the major components (compiler,
  293.      kernel, and so on) of the operating system on which the executable
  294.      runs, unless that component itself accompanies the executable.
  295.  
  296.      If distribution of executable or object code is made by offering
  297.      access to copy from a designated place, then offering equivalent
  298.      access to copy the source code from the same place counts as
  299.      distribution of the source code, even though third parties are not
  300.      compelled to copy the source along with the object code.
  301.  
  302.   5. You may not copy, modify, sublicense, or distribute the Program
  303.      except as expressly provided under this License.  Any attempt
  304.      otherwise to copy, modify, sublicense or distribute the Program is
  305.      void, and will automatically terminate your rights under this
  306.      License. However, parties who have received copies, or rights,
  307.      from you under this License will not have their licenses
  308.      terminated so long as such parties remain in full compliance.
  309.  
  310.   6. You are not required to accept this License, since you have not
  311.      signed it.  However, nothing else grants you permission to modify
  312.      or distribute the Program or its derivative works.  These actions
  313.      are prohibited by law if you do not accept this License. 
  314.      Therefore, by modifying or distributing the Program (or any work
  315.      based on the Program), you indicate your acceptance of this
  316.      License to do so, and all its terms and conditions for copying,
  317.      distributing or modifying the Program or works based on it.
  318.  
  319.   7. Each time you redistribute the Program (or any work based on the
  320.      Program), the recipient automatically receives a license from the
  321.      original licensor to copy, distribute or modify the Program
  322.      subject to these terms and conditions.  You may not impose any
  323.      further restrictions on the recipients' exercise of the rights
  324.      granted herein. You are not responsible for enforcing compliance
  325.      by third parties to this License.
  326.  
  327.   8. If, as a consequence of a court judgment or allegation of patent
  328.      infringement or for any other reason (not limited to patent
  329.      issues), conditions are imposed on you (whether by court order,
  330.      agreement or otherwise) that contradict the conditions of this
  331.      License, they do not excuse you from the conditions of this
  332.      License.  If you cannot distribute so as to satisfy simultaneously
  333.      your obligations under this License and any other pertinent
  334.      obligations, then as a consequence you may not distribute the
  335.      Program at all.  For example, if a patent license would not permit
  336.      royalty-free redistribution of the Program by all those who
  337.      receive copies directly or indirectly through you, then the only
  338.      way you could satisfy both it and this License would be to refrain
  339.      entirely from distribution of the Program.
  340.  
  341.      If any portion of this section is held invalid or unenforceable
  342.      under any particular circumstance, the balance of the section is
  343.      intended to apply and the section as a whole is intended to apply
  344.      in other circumstances.
  345.  
  346.      It is not the purpose of this section to induce you to infringe any
  347.      patents or other property right claims or to contest validity of
  348.      any such claims; this section has the sole purpose of protecting
  349.      the integrity of the free software distribution system, which is
  350.      implemented by public license practices.  Many people have made
  351.      generous contributions to the wide range of software distributed
  352.      through that system in reliance on consistent application of that
  353.      system; it is up to the author/donor to decide if he or she is
  354.      willing to distribute software through any other system and a
  355.      licensee cannot impose that choice.
  356.  
  357.      This section is intended to make thoroughly clear what is believed
  358.      to be a consequence of the rest of this License.
  359.  
  360.   9. If the distribution and/or use of the Program is restricted in
  361.      certain countries either by patents or by copyrighted interfaces,
  362.      the original copyright holder who places the Program under this
  363.      License may add an explicit geographical distribution limitation
  364.      excluding those countries, so that distribution is permitted only
  365.      in or among countries not thus excluded.  In such case, this
  366.      License incorporates the limitation as if written in the body of
  367.      this License.
  368.  
  369.  10. The Free Software Foundation may publish revised and/or new
  370.      versions of the General Public License from time to time.  Such
  371.      new versions will be similar in spirit to the present version, but
  372.      may differ in detail to address new problems or concerns.
  373.  
  374.      Each version is given a distinguishing version number.  If the
  375.      Program specifies a version number of this License which applies
  376.      to it and "any later version", you have the option of following
  377.      the terms and conditions either of that version or of any later
  378.      version published by the Free Software Foundation.  If the Program
  379.      does not specify a version number of this License, you may choose
  380.      any version ever published by the Free Software Foundation.
  381.  
  382.  11. If you wish to incorporate parts of the Program into other free
  383.      programs whose distribution conditions are different, write to the
  384.      author to ask for permission.  For software which is copyrighted
  385.      by the Free Software Foundation, write to the Free Software
  386.      Foundation; we sometimes make exceptions for this.  Our decision
  387.      will be guided by the two goals of preserving the free status of
  388.      all derivatives of our free software and of promoting the sharing
  389.      and reuse of software generally.
  390.  
  391.                                 NO WARRANTY
  392.  
  393.  12. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
  394.      WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
  395.      LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  396.      HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
  397.      WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT
  398.      NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  399.      FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE
  400.      QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  401.      PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  402.      SERVICING, REPAIR OR CORRECTION.
  403.  
  404.  13. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  405.      WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
  406.      MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
  407.      LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
  408.      INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  409.      INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  410.      DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
  411.      OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
  412.      OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  413.      ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  414.  
  415.                       END OF TERMS AND CONDITIONS
  416.  
  417. How to Apply These Terms to Your New Programs
  418. =============================================
  419.  
  420.    If you develop a new program, and you want it to be of the greatest
  421. possible use to the public, the best way to achieve this is to make it
  422. free software which everyone can redistribute and change under these
  423. terms.
  424.  
  425.    To do so, attach the following notices to the program.  It is safest
  426. to attach them to the start of each source file to most effectively
  427. convey the exclusion of warranty; and each file should have at least
  428. the "copyright" line and a pointer to where the full notice is found.
  429.  
  430.      ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
  431.      Copyright (C) 19YY  NAME OF AUTHOR
  432.      
  433.      This program is free software; you can redistribute it and/or modify
  434.      it under the terms of the GNU General Public License as published by
  435.      the Free Software Foundation; either version 2 of the License, or
  436.      (at your option) any later version.
  437.      
  438.      This program is distributed in the hope that it will be useful,
  439.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  440.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  441.      GNU General Public License for more details.
  442.      
  443.      You should have received a copy of the GNU General Public License
  444.      along with this program; if not, write to the Free Software
  445.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  446.  
  447.    Also add information on how to contact you by electronic and paper
  448. mail.
  449.  
  450.    If the program is interactive, make it output a short notice like
  451. this when it starts in an interactive mode:
  452.  
  453.      Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR
  454.      Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  455.      This is free software, and you are welcome to redistribute it
  456.      under certain conditions; type `show c' for details.
  457.  
  458.    The hypothetical commands `show w' and `show c' should show the
  459. appropriate parts of the General Public License.  Of course, the
  460. commands you use may be called something other than `show w' and `show
  461. c'; they could even be mouse-clicks or menu items--whatever suits your
  462. program.
  463.  
  464.    You should also get your employer (if you work as a programmer) or
  465. your school, if any, to sign a "copyright disclaimer" for the program,
  466. if necessary.  Here is a sample; alter the names:
  467.  
  468.      Yoyodyne, Inc., hereby disclaims all copyright
  469.      interest in the program `Gnomovision'
  470.      (which makes passes at compilers) written
  471.      by James Hacker.
  472.      
  473.      SIGNATURE OF TY COON, 1 April 1989
  474.      Ty Coon, President of Vice
  475.  
  476.    This General Public License does not permit incorporating your
  477. program into proprietary programs.  If your program is a subroutine
  478. library, you may consider it more useful to permit linking proprietary
  479. applications with the library.  If this is what you want to do, use the
  480. GNU Library General Public License instead of this License.
  481.  
  482. File: bison.info,  Node: Concepts,  Next: Examples,  Prev: Copying,  Up: Top
  483.  
  484. The Concepts of Bison
  485. *********************
  486.  
  487.    This chapter introduces many of the basic concepts without which the
  488. details of Bison will not make sense.  If you do not already know how to
  489. use Bison or Yacc, we suggest you start by reading this chapter
  490. carefully.
  491.  
  492. * Menu:
  493.  
  494. * Language and Grammar::  Languages and context-free grammars,
  495.                             as mathematical ideas.
  496. * Grammar in Bison::      How we represent grammars for Bison's sake.
  497. * Semantic Values::       Each token or syntactic grouping can have
  498.                             a semantic value (the value of an integer,
  499.                             the name of an identifier, etc.).
  500. * Semantic Actions::      Each rule can have an action containing C code.
  501. * Bison Parser::          What are Bison's input and output,
  502.                             how is the output used?
  503. * Stages::                Stages in writing and running Bison grammars.
  504. * Grammar Layout::        Overall structure of a Bison grammar file.
  505.  
  506. File: bison.info,  Node: Language and Grammar,  Next: Grammar in Bison,  Prev: Concepts,  Up: Concepts
  507.  
  508. Languages and Context-Free Grammars
  509. ===================================
  510.  
  511.    In order for Bison to parse a language, it must be described by a
  512. "context-free grammar".  This means that you specify one or more
  513. "syntactic groupings" and give rules for constructing them from their
  514. parts.  For example, in the C language, one kind of grouping is called
  515. an `expression'.  One rule for making an expression might be, "An
  516. expression can be made of a minus sign and another expression". 
  517. Another would be, "An expression can be an integer".  As you can see,
  518. rules are often recursive, but there must be at least one rule which
  519. leads out of the recursion.
  520.  
  521.    The most common formal system for presenting such rules for humans
  522. to read is "Backus-Naur Form" or "BNF", which was developed in order to
  523. specify the language Algol 60.  Any grammar expressed in BNF is a
  524. context-free grammar.  The input to Bison is essentially
  525. machine-readable BNF.
  526.  
  527.    Not all context-free languages can be handled by Bison, only those
  528. that are LALR(1).  In brief, this means that it must be possible to
  529. tell how to parse any portion of an input string with just a single
  530. token of look-ahead.  Strictly speaking, that is a description of an
  531. LR(1) grammar, and LALR(1) involves additional restrictions that are
  532. hard to explain simply; but it is rare in actual practice to find an
  533. LR(1) grammar that fails to be LALR(1).  *Note  Mysterious
  534. Reduce/Reduce Conflicts: Mystery Conflicts, for more information on
  535. this.
  536.  
  537.    In the formal grammatical rules for a language, each kind of
  538. syntactic unit or grouping is named by a "symbol".  Those which are
  539. built by grouping smaller constructs according to grammatical rules are
  540. called "nonterminal symbols"; those which can't be subdivided are called
  541. "terminal symbols" or "token types".  We call a piece of input
  542. corresponding to a single terminal symbol a "token", and a piece
  543. corresponding to a single nonterminal symbol a "grouping".
  544.  
  545.    We can use the C language as an example of what symbols, terminal and
  546. nonterminal, mean.  The tokens of C are identifiers, constants (numeric
  547. and string), and the various keywords, arithmetic operators and
  548. punctuation marks.  So the terminal symbols of a grammar for C include
  549. `identifier', `number', `string', plus one symbol for each keyword,
  550. operator or punctuation mark: `if', `return', `const', `static', `int',
  551. `char', `plus-sign', `open-brace', `close-brace', `comma' and many
  552. more.  (These tokens can be subdivided into characters, but that is a
  553. matter of lexicography, not grammar.)
  554.  
  555.    Here is a simple C function subdivided into tokens:
  556.  
  557.      int             /* keyword `int' */
  558.      square (x)      /* identifier, open-paren, */
  559.                      /* identifier, close-paren */
  560.           int x;     /* keyword `int', identifier, semicolon */
  561.      {               /* open-brace */
  562.        return x * x; /* keyword `return', identifier, */
  563.                      /* asterisk, identifier, semicolon */
  564.      }               /* close-brace */
  565.  
  566.    The syntactic groupings of C include the expression, the statement,
  567. the declaration, and the function definition.  These are represented in
  568. the grammar of C by nonterminal symbols `expression', `statement',
  569. `declaration' and `function definition'.  The full grammar uses dozens
  570. of additional language constructs, each with its own nonterminal
  571. symbol, in order to express the meanings of these four.  The example
  572. above is a function definition; it contains one declaration, and one
  573. statement.  In the statement, each `x' is an expression and so is `x *
  574. x'.
  575.  
  576.    Each nonterminal symbol must have grammatical rules showing how it
  577. is made out of simpler constructs.  For example, one kind of C
  578. statement is the `return' statement; this would be described with a
  579. grammar rule which reads informally as follows:
  580.  
  581.      A `statement' can be made of a `return' keyword, an `expression'
  582.      and a `semicolon'.
  583.  
  584. There would be many other rules for `statement', one for each kind of
  585. statement in C.
  586.  
  587.    One nonterminal symbol must be distinguished as the special one which
  588. defines a complete utterance in the language.  It is called the "start
  589. symbol".  In a compiler, this means a complete input program.  In the C
  590. language, the nonterminal symbol `sequence of definitions and
  591. declarations' plays this role.
  592.  
  593.    For example, `1 + 2' is a valid C expression--a valid part of a C
  594. program--but it is not valid as an *entire* C program.  In the
  595. context-free grammar of C, this follows from the fact that `expression'
  596. is not the start symbol.
  597.  
  598.    The Bison parser reads a sequence of tokens as its input, and groups
  599. the tokens using the grammar rules.  If the input is valid, the end
  600. result is that the entire token sequence reduces to a single grouping
  601. whose symbol is the grammar's start symbol.  If we use a grammar for C,
  602. the entire input must be a `sequence of definitions and declarations'. 
  603. If not, the parser reports a syntax error.
  604.  
  605. File: bison.info,  Node: Grammar in Bison,  Next: Semantic Values,  Prev: Language and Grammar,  Up: Concepts
  606.  
  607. From Formal Rules to Bison Input
  608. ================================
  609.  
  610.    A formal grammar is a mathematical construct.  To define the language
  611. for Bison, you must write a file expressing the grammar in Bison syntax:
  612. a "Bison grammar" file.  *Note Grammar File::.
  613.  
  614.    A nonterminal symbol in the formal grammar is represented in Bison
  615. input as an identifier, like an identifier in C.  By convention, it
  616. should be in lower case, such as `expr', `stmt' or `declaration'.
  617.  
  618.    The Bison representation for a terminal symbol is also called a
  619. "token type".  Token types as well can be represented as C-like
  620. identifiers.  By convention, these identifiers should be upper case to
  621. distinguish them from nonterminals: for example, `INTEGER',
  622. `IDENTIFIER', `IF' or `RETURN'.  A terminal symbol that stands for a
  623. particular keyword in the language should be named after that keyword
  624. converted to upper case. The terminal symbol `error' is reserved for
  625. error recovery. *Note Symbols::.
  626.  
  627.    A terminal symbol can also be represented as a character literal,
  628. just like a C character constant.  You should do this whenever a token
  629. is just a single character (parenthesis, plus-sign, etc.): use that
  630. same character in a literal as the terminal symbol for that token.
  631.  
  632.    The grammar rules also have an expression in Bison syntax.  For
  633. example, here is the Bison rule for a C `return' statement.  The
  634. semicolon in quotes is a literal character token, representing part of
  635. the C syntax for the statement; the naked semicolon, and the colon, are
  636. Bison punctuation used in every rule.
  637.  
  638.      stmt:   RETURN expr ';'
  639.              ;
  640.  
  641. *Note Rules::.
  642.  
  643. File: bison.info,  Node: Semantic Values,  Next: Semantic Actions,  Prev: Grammar in Bison,  Up: Concepts
  644.  
  645. Semantic Values
  646. ===============
  647.  
  648.    A formal grammar selects tokens only by their classifications: for
  649. example, if a rule mentions the terminal symbol `integer constant', it
  650. means that *any* integer constant is grammatically valid in that
  651. position.  The precise value of the constant is irrelevant to how to
  652. parse the input: if `x+4' is grammatical then `x+1' or `x+3989' is
  653. equally grammatical.
  654.  
  655.    But the precise value is very important for what the input means
  656. once it is parsed.  A compiler is useless if it fails to distinguish
  657. between 4, 1 and 3989 as constants in the program!  Therefore, each
  658. token in a Bison grammar has both a token type and a "semantic value". 
  659. *Note Semantics::, for details.
  660.  
  661.    The token type is a terminal symbol defined in the grammar, such as
  662. `INTEGER', `IDENTIFIER' or `',''.  It tells everything you need to know
  663. to decide where the token may validly appear and how to group it with
  664. other tokens.  The grammar rules know nothing about tokens except their
  665. types.
  666.  
  667.    The semantic value has all the rest of the information about the
  668. meaning of the token, such as the value of an integer, or the name of an
  669. identifier.  (A token such as `','' which is just punctuation doesn't
  670. need to have any semantic value.)
  671.  
  672.    For example, an input token might be classified as token type
  673. `INTEGER' and have the semantic value 4.  Another input token might
  674. have the same token type `INTEGER' but value 3989.  When a grammar rule
  675. says that `INTEGER' is allowed, either of these tokens is acceptable
  676. because each is an `INTEGER'.  When the parser accepts the token, it
  677. keeps track of the token's semantic value.
  678.  
  679.    Each grouping can also have a semantic value as well as its
  680. nonterminal symbol.  For example, in a calculator, an expression
  681. typically has a semantic value that is a number.  In a compiler for a
  682. programming language, an expression typically has a semantic value that
  683. is a tree structure describing the meaning of the expression.
  684.  
  685. File: bison.info,  Node: Semantic Actions,  Next: Bison Parser,  Prev: Semantic Values,  Up: Concepts
  686.  
  687. Semantic Actions
  688. ================
  689.  
  690.    In order to be useful, a program must do more than parse input; it
  691. must also produce some output based on the input.  In a Bison grammar,
  692. a grammar rule can have an "action" made up of C statements.  Each time
  693. the parser recognizes a match for that rule, the action is executed.
  694. *Note Actions::.
  695.  
  696.    Most of the time, the purpose of an action is to compute the
  697. semantic value of the whole construct from the semantic values of its
  698. parts.  For example, suppose we have a rule which says an expression
  699. can be the sum of two expressions.  When the parser recognizes such a
  700. sum, each of the subexpressions has a semantic value which describes
  701. how it was built up. The action for this rule should create a similar
  702. sort of value for the newly recognized larger expression.
  703.  
  704.    For example, here is a rule that says an expression can be the sum of
  705. two subexpressions:
  706.  
  707.      expr: expr '+' expr   { $$ = $1 + $3; }
  708.              ;
  709.  
  710. The action says how to produce the semantic value of the sum expression
  711. from the values of the two subexpressions.
  712.  
  713. File: bison.info,  Node: Bison Parser,  Next: Stages,  Prev: Semantic Actions,  Up: Concepts
  714.  
  715. Bison Output: the Parser File
  716. =============================
  717.  
  718.    When you run Bison, you give it a Bison grammar file as input.  The
  719. output is a C source file that parses the language described by the
  720. grammar. This file is called a "Bison parser".  Keep in mind that the
  721. Bison utility and the Bison parser are two distinct programs: the Bison
  722. utility is a program whose output is the Bison parser that becomes part
  723. of your program.
  724.  
  725.    The job of the Bison parser is to group tokens into groupings
  726. according to the grammar rules--for example, to build identifiers and
  727. operators into expressions.  As it does this, it runs the actions for
  728. the grammar rules it uses.
  729.  
  730.    The tokens come from a function called the "lexical analyzer" that
  731. you must supply in some fashion (such as by writing it in C).  The
  732. Bison parser calls the lexical analyzer each time it wants a new token.
  733.  It doesn't know what is "inside" the tokens (though their semantic
  734. values may reflect this).  Typically the lexical analyzer makes the
  735. tokens by parsing characters of text, but Bison does not depend on
  736. this.  *Note Lexical::.
  737.  
  738.    The Bison parser file is C code which defines a function named
  739. `yyparse' which implements that grammar.  This function does not make a
  740. complete C program: you must supply some additional functions.  One is
  741. the lexical analyzer.  Another is an error-reporting function which the
  742. parser calls to report an error.  In addition, a complete C program must
  743. start with a function called `main'; you have to provide this, and
  744. arrange for it to call `yyparse' or the parser will never run. *Note
  745. Interface::.
  746.  
  747.    Aside from the token type names and the symbols in the actions you
  748. write, all variable and function names used in the Bison parser file
  749. begin with `yy' or `YY'.  This includes interface functions such as the
  750. lexical analyzer function `yylex', the error reporting function
  751. `yyerror' and the parser function `yyparse' itself. This also includes
  752. numerous identifiers used for internal purposes. Therefore, you should
  753. avoid using C identifiers starting with `yy' or `YY' in the Bison
  754. grammar file except for the ones defined in this manual.
  755.  
  756. File: bison.info,  Node: Stages,  Next: Grammar Layout,  Prev: Bison Parser,  Up: Concepts
  757.  
  758. Stages in Using Bison
  759. =====================
  760.  
  761.    The actual language-design process using Bison, from grammar
  762. specification to a working compiler or interpreter, has these parts:
  763.  
  764.   1. Formally specify the grammar in a form recognized by Bison (*note
  765.      Grammar File::.).  For each grammatical rule in the language,
  766.      describe the action that is to be taken when an instance of that
  767.      rule is recognized.  The action is described by a sequence of C
  768.      statements.
  769.  
  770.   2. Write a lexical analyzer to process input and pass tokens to the
  771.      parser.  The lexical analyzer may be written by hand in C (*note
  772.      Lexical::.).  It could also be produced using Lex, but the use of
  773.      Lex is not discussed in this manual.
  774.  
  775.   3. Write a controlling function that calls the Bison-produced parser.
  776.  
  777.   4. Write error-reporting routines.
  778.  
  779.    To turn this source code as written into a runnable program, you
  780. must follow these steps:
  781.  
  782.   1. Run Bison on the grammar to produce the parser.
  783.  
  784.   2. Compile the code output by Bison, as well as any other source
  785.      files.
  786.  
  787.   3. Link the object files to produce the finished product.
  788.  
  789. File: bison.info,  Node: Grammar Layout,  Prev: Stages,  Up: Concepts
  790.  
  791. The Overall Layout of a Bison Grammar
  792. =====================================
  793.  
  794.    The input file for the Bison utility is a "Bison grammar file".  The
  795. general form of a Bison grammar file is as follows:
  796.  
  797.      %{
  798.      C DECLARATIONS
  799.      %}
  800.      
  801.      BISON DECLARATIONS
  802.      
  803.      %%
  804.      GRAMMAR RULES
  805.      %%
  806.      ADDITIONAL C CODE
  807.  
  808. The `%%', `%{' and `%}' are punctuation that appears in every Bison
  809. grammar file to separate the sections.
  810.  
  811.    The C declarations may define types and variables used in the
  812. actions. You can also use preprocessor commands to define macros used
  813. there, and use `#include' to include header files that do any of these
  814. things.
  815.  
  816.    The Bison declarations declare the names of the terminal and
  817. nonterminal symbols, and may also describe operator precedence and the
  818. data types of semantic values of various symbols.
  819.  
  820.    The grammar rules define how to construct each nonterminal symbol
  821. from its parts.
  822.  
  823.    The additional C code can contain any C code you want to use.  Often
  824. the definition of the lexical analyzer `yylex' goes here, plus
  825. subroutines called by the actions in the grammar rules.  In a simple
  826. program, all the rest of the program can go here.
  827.  
  828. File: bison.info,  Node: Examples,  Next: Grammar File,  Prev: Concepts,  Up: Top
  829.  
  830. Examples
  831. ********
  832.  
  833.    Now we show and explain three sample programs written using Bison: a
  834. reverse polish notation calculator, an algebraic (infix) notation
  835. calculator, and a multi-function calculator.  All three have been tested
  836. under BSD Unix 4.3; each produces a usable, though limited, interactive
  837. desk-top calculator.
  838.  
  839.    These examples are simple, but Bison grammars for real programming
  840. languages are written the same way. You can copy these examples out of
  841. the Info file and into a source file to try them.
  842.  
  843. * Menu:
  844.  
  845. * RPN Calc::               Reverse polish notation calculator;
  846.                              a first example with no operator precedence.
  847. * Infix Calc::             Infix (algebraic) notation calculator.
  848.                              Operator precedence is introduced.
  849. * Simple Error Recovery::  Continuing after syntax errors.
  850. * Multi-function Calc::    Calculator with memory and trig functions.
  851.                              It uses multiple data-types for semantic values.
  852. * Exercises::              Ideas for improving the multi-function calculator.
  853.  
  854. File: bison.info,  Node: RPN Calc,  Next: Infix Calc,  Prev: Examples,  Up: Examples
  855.  
  856. Reverse Polish Notation Calculator
  857. ==================================
  858.  
  859.    The first example is that of a simple double-precision "reverse
  860. polish notation" calculator (a calculator using postfix operators). 
  861. This example provides a good starting point, since operator precedence
  862. is not an issue. The second example will illustrate how operator
  863. precedence is handled.
  864.  
  865.    The source code for this calculator is named `rpcalc.y'.  The `.y'
  866. extension is a convention used for Bison input files.
  867.  
  868. * Menu:
  869.  
  870. * Decls: Rpcalc Decls.    Bison and C declarations for rpcalc.
  871. * Rules: Rpcalc Rules.    Grammar Rules for rpcalc, with explanation.
  872. * Input: Rpcalc Input.    Explaining the rules for `input'.
  873. * Line: Rpcalc Line.      Explaining the rules for `line'.
  874. * Expr: Rpcalc Expr.      Explaining the rules for `expr'.
  875. * Lexer: Rpcalc Lexer.    The lexical analyzer.
  876. * Main: Rpcalc Main.      The controlling function.
  877. * Error: Rpcalc Error.    The error reporting function.
  878. * Gen: Rpcalc Gen.        Running Bison on the grammar file.
  879. * Comp: Rpcalc Compile.   Run the C compiler on the output code.
  880.  
  881. File: bison.info,  Node: Rpcalc Decls,  Next: Rpcalc Rules,  Prev: RPN calc,  Up: RPN calc
  882.  
  883. Declarations for `rpcalc'
  884. -------------------------
  885.  
  886.    Here are the C and Bison declarations for the reverse polish notation
  887. calculator.  As in C, comments are placed between `/*...*/'.
  888.  
  889.      /* Reverse polish notation calculator. */
  890.      
  891.      %{
  892.      #define YYSTYPE double
  893.      #include <math.h>
  894.      %}
  895.      
  896.      %token NUM
  897.      
  898.      %% /* Grammar rules and actions follow */
  899.  
  900.    The C declarations section (*note C Declarations::.) contains two
  901. preprocessor directives.
  902.  
  903.    The `#define' directive defines the macro `YYSTYPE', thus specifying
  904. the C data type for semantic values of both tokens and groupings (*note
  905. Value Type::.).  The Bison parser will use whatever type `YYSTYPE' is
  906. defined as; if you don't define it, `int' is the default.  Because we
  907. specify `double', each token and each expression has an associated
  908. value, which is a floating point number.
  909.  
  910.    The `#include' directive is used to declare the exponentiation
  911. function `pow'.
  912.  
  913.    The second section, Bison declarations, provides information to
  914. Bison about the token types (*note Bison Declarations::.).  Each
  915. terminal symbol that is not a single-character literal must be declared
  916. here.  (Single-character literals normally don't need to be declared.) 
  917. In this example, all the arithmetic operators are designated by
  918. single-character literals, so the only terminal symbol that needs to be
  919. declared is `NUM', the token type for numeric constants.
  920.  
  921. File: bison.info,  Node: Rpcalc Rules,  Next: Rpcalc Input,  Prev: Rpcalc Decls,  Up: RPN Calc
  922.  
  923. Grammar Rules for `rpcalc'
  924. --------------------------
  925.  
  926.    Here are the grammar rules for the reverse polish notation
  927. calculator.
  928.  
  929.      input:    /* empty */
  930.              | input line
  931.      ;
  932.      
  933.      line:     '\n'
  934.              | exp '\n'  { printf ("\t%.10g\n", $1); }
  935.      ;
  936.      
  937.      exp:      NUM             { $$ = $1;         }
  938.              | exp exp '+'     { $$ = $1 + $2;    }
  939.              | exp exp '-'     { $$ = $1 - $2;    }
  940.              | exp exp '*'     { $$ = $1 * $2;    }
  941.              | exp exp '/'     { $$ = $1 / $2;    }
  942.            /* Exponentiation */
  943.              | exp exp '^'     { $$ = pow ($1, $2); }
  944.            /* Unary minus    */
  945.              | exp 'n'         { $$ = -$1;        }
  946.      ;
  947.      %%
  948.  
  949.    The groupings of the rpcalc "language" defined here are the
  950. expression (given the name `exp'), the line of input (`line'), and the
  951. complete input transcript (`input').  Each of these nonterminal symbols
  952. has several alternate rules, joined by the `|' punctuator which is read
  953. as "or".  The following sections explain what these rules mean.
  954.  
  955.    The semantics of the language is determined by the actions taken
  956. when a grouping is recognized.  The actions are the C code that appears
  957. inside braces.  *Note Actions::.
  958.  
  959.    You must specify these actions in C, but Bison provides the means for
  960. passing semantic values between the rules.  In each action, the
  961. pseudo-variable `$$' stands for the semantic value for the grouping
  962. that the rule is going to construct.  Assigning a value to `$$' is the
  963. main job of most actions.  The semantic values of the components of the
  964. rule are referred to as `$1', `$2', and so on.
  965.  
  966. File: bison.info,  Node: Rpcalc Input,  Next: Rpcalc Line,  Prev: Rpcalc Rules,  Up: RPN Calc
  967.  
  968. Explanation of `input'
  969. ......................
  970.  
  971.    Consider the definition of `input':
  972.  
  973.      input:    /* empty */
  974.              | input line
  975.      ;
  976.  
  977.    This definition reads as follows: "A complete input is either an
  978. empty string, or a complete input followed by an input line".  Notice
  979. that "complete input" is defined in terms of itself.  This definition
  980. is said to be "left recursive" since `input' appears always as the
  981. leftmost symbol in the sequence.  *Note Recursion::.
  982.  
  983.    The first alternative is empty because there are no symbols between
  984. the colon and the first `|'; this means that `input' can match an empty
  985. string of input (no tokens).  We write the rules this way because it is
  986. legitimate to type `Ctrl-d' right after you start the calculator. It's
  987. conventional to put an empty alternative first and write the comment
  988. `/* empty */' in it.
  989.  
  990.    The second alternate rule (`input line') handles all nontrivial
  991. input. It means, "After reading any number of lines, read one more line
  992. if possible."  The left recursion makes this rule into a loop.  Since
  993. the first alternative matches empty input, the loop can be executed
  994. zero or more times.
  995.  
  996.    The parser function `yyparse' continues to process input until a
  997. grammatical error is seen or the lexical analyzer says there are no more
  998. input tokens; we will arrange for the latter to happen at end of file.
  999.  
  1000. File: bison.info,  Node: Rpcalc Line,  Next: Rpcalc Expr,  Prev: Rpcalc Input,  Up: RPN Calc
  1001.  
  1002. Explanation of `line'
  1003. .....................
  1004.  
  1005.    Now consider the definition of `line':
  1006.  
  1007.      line:     '\n'
  1008.              | exp '\n'  { printf ("\t%.10g\n", $1); }
  1009.      ;
  1010.  
  1011.    The first alternative is a token which is a newline character; this
  1012. means that rpcalc accepts a blank line (and ignores it, since there is
  1013. no action).  The second alternative is an expression followed by a
  1014. newline. This is the alternative that makes rpcalc useful.  The
  1015. semantic value of the `exp' grouping is the value of `$1' because the
  1016. `exp' in question is the first symbol in the alternative.  The action
  1017. prints this value, which is the result of the computation the user
  1018. asked for.
  1019.  
  1020.    This action is unusual because it does not assign a value to `$$'. 
  1021. As a consequence, the semantic value associated with the `line' is
  1022. uninitialized (its value will be unpredictable).  This would be a bug if
  1023. that value were ever used, but we don't use it: once rpcalc has printed
  1024. the value of the user's input line, that value is no longer needed.
  1025.  
  1026. File: bison.info,  Node: Rpcalc Expr,  Next: Rpcalc Lexer,  Prev: Rpcalc Line,  Up: RPN Calc
  1027.  
  1028. Explanation of `expr'
  1029. .....................
  1030.  
  1031.    The `exp' grouping has several rules, one for each kind of
  1032. expression. The first rule handles the simplest expressions: those that
  1033. are just numbers. The second handles an addition-expression, which
  1034. looks like two expressions followed by a plus-sign.  The third handles
  1035. subtraction, and so on.
  1036.  
  1037.      exp:      NUM
  1038.              | exp exp '+'     { $$ = $1 + $2;    }
  1039.              | exp exp '-'     { $$ = $1 - $2;    }
  1040.              ...
  1041.              ;
  1042.  
  1043.    We have used `|' to join all the rules for `exp', but we could
  1044. equally well have written them separately:
  1045.  
  1046.      exp:      NUM ;
  1047.      exp:      exp exp '+'     { $$ = $1 + $2;    } ;
  1048.      exp:      exp exp '-'     { $$ = $1 - $2;    } ;
  1049.              ...
  1050.  
  1051.    Most of the rules have actions that compute the value of the
  1052. expression in terms of the value of its parts.  For example, in the
  1053. rule for addition, `$1' refers to the first component `exp' and `$2'
  1054. refers to the second one.  The third component, `'+'', has no meaningful
  1055. associated semantic value, but if it had one you could refer to it as
  1056. `$3'.  When `yyparse' recognizes a sum expression using this rule, the
  1057. sum of the two subexpressions' values is produced as the value of the
  1058. entire expression.  *Note Actions::.
  1059.  
  1060.    You don't have to give an action for every rule.  When a rule has no
  1061. action, Bison by default copies the value of `$1' into `$$'. This is
  1062. what happens in the first rule (the one that uses `NUM').
  1063.  
  1064.    The formatting shown here is the recommended convention, but Bison
  1065. does not require it.  You can add or change whitespace as much as you
  1066. wish. For example, this:
  1067.  
  1068.      exp   : NUM | exp exp '+' {$$ = $1 + $2; } | ...
  1069.  
  1070. means the same thing as this:
  1071.  
  1072.      exp:      NUM
  1073.              | exp exp '+'    { $$ = $1 + $2; }
  1074.              | ...
  1075.  
  1076. The latter, however, is much more readable.
  1077.